home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / Moofwars 1.02 / MoofWars Sprocket / •Headers / TSprite.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-05  |  8.9 KB  |  262 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. TSprite.h
  3.  
  4. This implements a first cut at a standardized sprite class.  This is an abstract base
  5. class and all sprites must be subclassed off a TSprite.
  6.  
  7. Author: Timothy Carroll
  8. Apple Developer Technical Support
  9. timc@apple.com
  10.  
  11. Modification History: 
  12.  
  13. 8/15/96        TMC     Initial Release
  14.  
  15. Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  16.  
  17. You may incorporate this sample code into your applications without
  18. restriction, though the sample code has been provided "AS IS" and the
  19. responsibility for its operation is 100% yours.  However, what you are
  20. not permitted to do is to redistribute the source as "DSC Sample Code"
  21. after having made changes. If you're going to re-distribute the source,
  22. we require that you make it clear in the source that the code was
  23. descended from Apple Sample Code, but that you've made changes.
  24.  
  25. *************************************************************************************/
  26. #ifndef _TSPRITE_
  27. #define _TSPRITE_
  28.  
  29. #pragma once
  30.  
  31. #include "TGraphicCollection.h"
  32. #include "Scaling.h"
  33.  
  34. #if PRAGMA_STRUCT_ALIGN
  35. #pragma options align=power
  36. #endif
  37.  
  38.  
  39.  
  40. enum eVisibleFlag
  41. {
  42.     kInvisible = 0,  // An invisible sprite can't be hit tested or drawn.
  43.     kVisible = 1
  44. };
  45.  
  46.  
  47.  
  48.  
  49.  
  50. // When creating a new TSprite, you fill in a TSpriteData structure and pass it along
  51. // to the TSprite constructor.  Two constructors are provided, one that takes a
  52. // TSpriteData pointer and one that takes a TSpriteData handle.
  53.  
  54. // The handle allows a nice method we can use to create a sprite from resource data.
  55. // Note that the TSprite constructor and any follow up constructors should not modify
  56. // the data passed back into it.  We'll define them as const parameters in th
  57. // constructor to enforce this.
  58.  
  59. // All the handle based constructor will do is dereference the handle and pass it in.
  60. // We'll lock and unlock the handle over the call.
  61.  
  62. struct TSpriteData {
  63.  // A 4 character type used to describe the sprite.  Each sprite class will register
  64.  // a OSType that can be used to create a sprite of that type.  Each subclass should
  65.  // also define a Data type that is used to define its parameters (each adding to the
  66.  // previous subclasses's data, obviously.
  67.  
  68.  // If you know what type of sprite you are creating, you can call the constructor
  69.  // directly.  Eventually, I'll try to provide a global creator that can be called with
  70.  // a data structure that will call the appropriate creator based on the spriteType field.
  71.  
  72.  
  73.     OSType                spriteType; 
  74.     SInt32                initialX; // sprite's location in world coordinates
  75.     SInt32                initialY;
  76.     SInt32                initialXVelocity; // sprite's velocity in world coordinates
  77.     SInt32              initialYVelocity;
  78.     SInt16                visibility;         // initial visibility
  79.     SInt16                face;                // initial face
  80.     
  81.     // Graphics to use for the sprite.
  82.     //If the sprites are already loaded, then we can pass in a copy of the TGraphicCollection
  83.     //directly.  If the sprites weren't loaded, preloadedCollection should be set to NULL.
  84.     //If it is NULL, then the resource ID of the collection must be passed in via collectionID.
  85.     //We do this to provide a fast way to make sprites without all the load time.
  86.     SInt16                collectionID;         
  87.     TGraphicCollection    *preloadedCollection;
  88. };
  89.  
  90.  
  91. // Scaling and clipping -- old stuff to be replaced.
  92.  
  93. /* 
  94. // This data structure is used to pass in all of the scaling information needed by each
  95. // TSprite.  Unlike the last class, this one we'll actually just define as a pointer -- no
  96. // resource based info. This might be a mistake, we can change it easily enough.
  97.  
  98. // Note that the point passed in via worldPt is mapped to the exact center of whatever Rect
  99. // is passed in.  This Rect is also the one used to set the clipping for the TGraphic class.
  100. struct TScalingData {
  101.     WorldPoint32    worldPt;
  102.     SInt32            scale;
  103.     Rect            screenRect;
  104. };
  105.  
  106. extern WorldRect32 gScreenBounds;
  107.  
  108. */
  109.  
  110. class TSpriteCollection;
  111. class TSprite;
  112.  
  113.  
  114.  
  115. class TSprite
  116. {
  117.     // TSpriteCollection is declared as a friend class because it needs direct access to 
  118.     // the "fNextSprite" and fPrevSprite members.  In the current implementation, we also use
  119.     // it in the hit testing code, although this will hopefully be changed in the future because
  120.     // I don't particularly like it. 
  121.  
  122.     friend TSpriteCollection;
  123.     
  124.     
  125.     public:
  126.     
  127.     enum {
  128.         kSpriteType = 'SPRT'
  129.     };
  130.     
  131.  
  132.     
  133.     protected:
  134.  
  135.     // This stores all the data for the sprite's current location and visibility.
  136.     SInt32                fCoordX;        // 16.16 fixed
  137.     SInt32                fCoordY;        // 16.16 fixed
  138.  
  139.     SInt32                fVelocityX;    // 16.16 fixed
  140.     SInt32              fVelocityY;    // 16.16 fixed
  141.  
  142.     UInt16                fFace;
  143.     UInt16                fVisibility;
  144.     
  145.     Rect                fBounds; // Cache this here rather than in the graphic collection.
  146.     SInt32                fXOffset;
  147.     SInt32                fYOffset; // used when drawing to move to the upper left corner.
  148.     
  149.     // These store the graphic collections we need to draw our sprites.
  150.     TGraphicCollection    *fSpriteImages;
  151.     
  152.     // These are used to maintain sprite groups.  fNextSprite and fPrevSprite
  153.     // are used by the owning TSpriteCollection and should not be touched by the Sprite classes at all.
  154.     
  155.     TSpriteCollection    *fGroup;          // if we are a member of a group, this will be non-null.
  156.     TSprite            *fNextSprite;     // used by TSpriteCollection if we are a group member.
  157.     TSprite            *fPrevSprite;     // used by TSpriteCollection if we are a group member.
  158.  
  159.     
  160.     
  161.     public:
  162.     
  163. /*************************************************************************************
  164.     Constructors and Destructors
  165.     
  166. *************************************************************************************/
  167.     
  168.     TSprite (TSpriteData *data);
  169.     ~TSprite (void);
  170.     
  171. /*************************************************************************************
  172.     Standard Accessor Functions
  173.     
  174. *************************************************************************************/
  175.     
  176.     OSType            GetSpriteType (void) {return kSpriteType;}
  177.     
  178.     void            GetCurrentWorldLocation (SInt32 *worldX, SInt32 *worldY);
  179.     void            SetCurrentWorldLocation (SInt32 worldX, SInt32 worldY);
  180.     
  181. /*    
  182. Boolean
  183. TSprite::GetCurrentScreenLocation (Rect *currentRect)
  184. {
  185. #pragma unused (currentRect)
  186.  
  187.     DebugStr ("\pNot implemented");
  188.     return false;
  189. }
  190. */
  191.     
  192.     
  193.     SInt16            GetFace() {return fFace;}
  194.     void            SetFace (SInt16 newFace) {fFace = newFace;}
  195.     
  196.     SInt16            GetVisibility () {return fVisibility;}
  197.     void            SetVisibility (SInt16 newVisibility) {fVisibility = newVisibility;}
  198.  
  199.     SInt32            GetXVelocity();
  200.     SInt32            GetYVelocity();
  201.     void            SetXVelocity (SInt32 xVelocity);
  202.     void            SetYVelocity (SInt32 yVelocity);
  203.     
  204. /*************************************************************************************
  205.     Standard Actions that sprites need to do.
  206. *************************************************************************************/
  207.  
  208.     // Drawing takes the sprite's location relative to the scaling values and draws it
  209.     // to the current buffer.  Usually called by the TSpriteCollection automatically.
  210.     
  211.     void DrawSprite (void);
  212.     
  213.     // Each sprite will have its process routine called once per frame -- remember that if
  214.     // things are slowing down, not every frame will be drawn, but all frames will be
  215.     // processed.  Usually called by the TSpriteCollection automatically.
  216.     
  217.     // Default process sprite just moves the sprite according to its velocity.
  218.     virtual void ProcessSprite (void) {    fCoordX += fVelocityX; fCoordY += fVelocityY;}
  219.     
  220.     // Whenever two sprites are actually collided, each sprite's collision routine is called
  221.     // with the other as a parameter.  Usually called by the TSpriteCollection automatically.
  222.     virtual void Collision (TSprite *theSprite);
  223.  
  224. /*************************************************************************************
  225.     Utility functions
  226.     
  227.     These aren't automatically called, but are simple utility functions that exist because
  228.     all sprites might need to do them.
  229.     
  230. *************************************************************************************/
  231.     void Bounce (WorldRect32 *bounceBounds);
  232.  
  233. /*************************************************************************************
  234.     These routines add and remove a sprite from a group.  Call these routines, don't call
  235.     the routines from the TSpriteCollection class -- they only exist for these functions to
  236.     call.  If you call the TSpriteCollection routines, then parameters in the TSprite class don't
  237.     always get set properly.
  238.     
  239. *************************************************************************************/
  240.         
  241.     void AddToGroup (TSpriteCollection *theGroup);
  242.     void RemoveFromGroup (void);
  243.  
  244. /*************************************************************************************
  245.     These routines are used to set all the clipping information
  246.     These routines set the scaling information and coordinate system.  It also sets the
  247.     appropriate clipping rectangle in the TGraphic class.  It doesn't change the GWorld
  248.     pointed to by the TGraphic and the caller must make sure this rectangle is within the
  249.     GWorld's actual rectangle or bad things will happen.
  250.     
  251. *************************************************************************************/    
  252.  
  253. };
  254.     
  255.  
  256. #if PRAGMA_STRUCT_ALIGN
  257. #pragma options align=reset
  258. #endif
  259.  
  260.  
  261.  
  262. #endif /* _TSPRITE_ */